package cn.edu.klmy.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.klmy.dao.IElectiveDao;
import cn.edu.klmy.models.elective.StudentCourseCredit;
import cn.edu.klmy.models.elective.StudentCourseElectiveCredit;
import cn.edu.klmy.models.param.SystemParam;

@Controller
@RequestMapping("/credit")
public class CreditController {

	@Value("#{dataSourceProperties['elective1Credit']}")
	private int elective1Credit;
	@Value("#{dataSourceProperties['elective2Credit']}")
	private int elective2Credit;

	@Resource(name = "electiveDao")
	private IElectiveDao electiveDao;

	public void setElectiveDao(IElectiveDao electiveDao) {
		this.electiveDao = electiveDao;
	}

	@RequestMapping("/value")
	@ResponseBody
	public Credits getCredits() {
		Credits sc = new Credits();
		sc.setElective1Credit(elective1Credit);
		sc.setElective2Credit(elective2Credit);
		return sc;
	}

	@RequestMapping("/{studentId}")
	@ResponseBody
	public List<StudentCreditItem> getStudentCredit(@PathVariable int studentId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        SystemParam systemParam = (SystemParam) session.getAttribute("param");
        int term = systemParam.getTerm();
        int section = systemParam.getSection();
		List<StudentCourseElectiveCredit> elective1s = electiveDao
				.getStudentAllElection(studentId, term, section, 1);
		List<StudentCourseElectiveCredit> elective2s = electiveDao
				.getStudentAllElection(studentId, term, section, 2);
		int maxCount = 0; 
		if (elective1s != null) {
			maxCount = elective1s.size();
		}
		if (elective2s != null && maxCount < elective2s.size()) {
			maxCount = elective2s.size();
		}
		List<StudentCreditItem> result = new ArrayList<StudentCreditItem>();
		for (int i = 0; i < maxCount; i++) {
			StudentCreditItem item = new StudentCreditItem();
			if (elective1s != null && i < elective1s.size()) {
				item.setElective1(elective1s.get(i));
			}
			if (elective2s != null && i < elective2s.size()) {
				item.setElective2(elective2s.get(i));
			}
			result.add(item);
		}
		return result;
	}

	@RequestMapping("/{schedulingId}/student-course-credit")
	@ResponseBody
	public List<StudentCourseCredit> getStudentCourseCredit(
			@PathVariable int schedulingId, @RequestParam int term,
			@RequestParam int section) {
		return electiveDao.getStudentCourseCredits(schedulingId);
	}

	class Credits {
		private int elective1Credit;
		private int elective2Credit;

		public int getElective1Credit() {
			return elective1Credit;
		}

		public void setElective1Credit(int elective1Credit) {
			this.elective1Credit = elective1Credit;
		}

		public int getElective2Credit() {
			return elective2Credit;
		}

		public void setElective2Credit(int elective2Credit) {
			this.elective2Credit = elective2Credit;
		}

	}

	class StudentCreditItem {
		private StudentCourseElectiveCredit elective1;
		private StudentCourseElectiveCredit elective2;

		public StudentCourseElectiveCredit getElective1() {
			return elective1;
		}

		public void setElective1(StudentCourseElectiveCredit elective1) {
			this.elective1 = elective1;
		}

		public StudentCourseElectiveCredit getElective2() {
			return elective2;
		}

		public void setElective2(StudentCourseElectiveCredit elective2) {
			this.elective2 = elective2;
		}
	}
//
//	/**
//	 * @author 陈瑞军
//	 */
//	class StudentCredit {
//		private int elective1Credit;
//		private int elective2Credit;
//		private List<StudentCourseElectiveCredit> elective1s;
//		private List<StudentCourseElectiveCredit> elective2s;
//
//		public List<StudentCourseElectiveCredit> getElective1s() {
//			return elective1s;
//		}
//
//		public void setElective1s(List<StudentCourseElectiveCredit> elective1s) {
//			this.elective1s = elective1s;
//		}
//
//		public List<StudentCourseElectiveCredit> getElective2s() {
//			return elective2s;
//		}
//
//		public void setElective2s(List<StudentCourseElectiveCredit> elective2s) {
//			this.elective2s = elective2s;
//		}
//
//		public int getElective1Credit() {
//			return elective1Credit;
//		}
//
//		public void setElective1Credit(int elective1Credit) {
//			this.elective1Credit = elective1Credit;
//		}
//
//		public int getElective2Credit() {
//			return elective2Credit;
//		}
//
//		public void setElective2Credit(int elective2Credit) {
//			this.elective2Credit = elective2Credit;
//		}
//	}
}
